package src.main.java.org.stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Stream;

/*
* stream流的常用方法_中间方法
* */
public class Test2 {
    public static void main(String[] args) {
        ArrayList<Double> scores = new ArrayList<>();
        Collections.addAll(scores,88.5,100.0,60.0,99.0,9.5,99.6,52.0);
        //需求1：找出成绩大于等于60分的数据，并升序后，再输出
        scores.stream().filter(s->s>=60).sorted().forEach(s-> System.out.println(s));

        ArrayList<Student> students = new ArrayList<>();
        Student s1 = new Student("蜘蛛精", 172, 26);
        Student s2 = new Student("蜘蛛精", 172, 26);
        Student s3 = new Student("张思", 171.4, 22);
        Student s4 = new Student("王伟", 172.2, 23);
        Student s5 = new Student("唐僧", 178.1, 24);
        Student s6 = new Student("孙悟空", 174, 25);
        Collections.addAll(students,s1,s2,s3,s4,s5,s6);
        //找出年龄大于23小于等于30的学生，并按照年龄降序输出
        students.stream().filter(s->s.getAge()>=23&&s.getAge()<=30)
                .sorted(((o1, o2) -> o2.getAge()- o1.getAge()))
                .forEach(s-> System.out.println(s));
        System.out.println("===========");
        //去除身高最高的3名学生，并输出
        students.stream().sorted(((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight())))
                .limit(3).forEach(s-> System.out.println(s));
        System.out.println("===========");
        //取出身高倒数的2名学生，并输出
        students.stream().sorted(((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight())))
                .skip(students.size()-2).forEach(System.out::println); //skip跳过若干个
        System.out.println("===========");
        //找出身高超过165的学生姓名，去除重复姓名  使用map distinct去重复，自定义类型的对象（希望内容一样就去重，需要重写hashCode和equals方法）
        students.stream().filter(student -> student.getHeight()>168).map(Student::getName)
                .distinct().forEach(s-> System.out.println(s));

        Stream<String> st1 = Stream.of("张三", "李四");
        Stream<String> st2 = Stream.of("王五", "赵六");
        Stream<String> concat = Stream.concat(st1, st2);
        concat.forEach(System.out::println);
    }
}
